package reports;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import model.entity.Item;
import model.entity.Product;
import model.entity.container.ProductGroup;
import model.entity.container.StorageUnit;
import model.management.ItemManager;
import model.management.StorageUnitManager;
import reports.builders.IBuilder;
import reports.display.ReportTable;
import reports.display.ReportTableRow;
import reports.visitor.IVisitor;

import common.util.DateUtils;

public class RemovedItemsReport extends Report
{
  private static final String[]     COLUMNS      = new String[]
                                                 {
      "Description	",
      "Size			",
      "Product Barcode",
      "Removed		",
      "Current Supply	",
                                                 };

  private static final String       TITLE        = "Removed Items Report";

  private IVisitor                  visitor;
  private ReportTable               table;
  private Date                      sinceDate;
  private HashMap<Product, Integer> removedItems = new HashMap<Product, Integer>();

  public RemovedItemsReport(Date sinceDate)
  {
    super(TITLE);
    table = new ReportTable(null, COLUMNS);
    visitor = new RemovedItemsReportVisitor();
    this.sinceDate = sinceDate;
  }

  @Override
  public void buildReport(IBuilder<?> builder)
  {
    builder.addTitle("Items Removed Since " + DateUtils.formatDateTime(sinceDate));
    builder.startTable(table.getColumnCount());
    builder.addTableHeader(table.getColumnTitles());
    Iterator<ReportTableRow> itr = table.getRows();
    while (itr.hasNext())
    {
      builder.addRow(itr.next().getCells());
    }
    builder.endTable();

  }

  @Override
  protected void visitData() throws Exception
  {
    StorageUnitManager.getInstance().preOrderAccept(visitor);
  }

  @Override
  protected void interpretData() throws Exception
  {

    removedItems = ItemManager.getInstance().getRemovedItems(sinceDate);

    for (Map.Entry<Product, Integer> key : removedItems.entrySet())
    {
      Product product = key.getKey();
      ReportTableRow row = table.createRow();
      row.setCell(0, product.getDescription());
      row.setCell(1, product.getSize());
      row.setCell(2, product.getBarcode().getCode());
      row.setCell(3, key.getValue());// for Removed (int)

      Iterator<StorageUnit> storageunits = StorageUnitManager.getStorageUnits();
      int count = 0;
      while (storageunits.hasNext())
      {
        StorageUnit su = storageunits.next();
        count = su.getItemCount(product);
      }
      row.setCell(4, count);// for Current Supply (int)
      table.addRow(row);

    }
  }

  private class RemovedItemsReportVisitor implements IVisitor
  {
    @Override
    public void visit(ProductGroup productgroup)
    {

    }

    @Override
    public void visit(StorageUnit storageunit)
    {

    }

    @Override
    public void visit(Item item)
    {
      // do nothing
    }

    @Override
    public void visit(Product product)
    {
      // do nothing
    }
  }
}
